const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ["一次性支付","50:50","50:30:20","自定义"],
    index:0,
    id:'',
    price:'',
    payStage:[{
      stage:'第一阶段',
      proportion:'100',
      amount:'',
      payRemarks:''
    }],
    customizeVal:'',
    submit_again:'',
    paytypeStatus:'',
    pay_type_status: '',//有无付款方式
    role: '',//角色
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var { payStage } = this.data
    var _this = this
    payStage[0].amount = options.servicePrice
    this.setData({
      id: options.taskid,
      price: options.servicePrice,
      payStage,
      paytypeStatus: options.paytypeStatus
    })
    // if (options.paytypeStatus == 1){
      var url = app.globalData.baseUrl + 'myTask/paySection';
      var data = {
        id: options.taskid,
        token: app.globalData.token
      }
      wx.showLoading({
        title: '加载中',
      })
      app.wxGetRequest(url, data, (res) => {
        console.log(res)
        wx.hideLoading()
        if (res.code == 1000) {
          if (res.data.pay_type_status != 0){
            payStage = [];
            var obj = {}
            var pay_section = res.data.pay_section
            for (var i = 0; i < pay_section.length; i++) {
              obj = {
                stage: pay_section[i].name,
                proportion: pay_section[i].percent,
                amount: pay_section[i].price,
                payRemarks: pay_section[i].desc
              }
              payStage.push(obj)
            }
            // 威客拒绝支付方式，雇主重新编辑提交submit_again等于1
            if (res.data.pay_type_status == 2 && res.data.role == 'employer'){
              _this.setData({
                submit_again:1
              })
            }
            _this.setData({
              index: res.data.pay_type - 1,
              pay_type_status: res.data.pay_type_status,
              role: res.data.role,
              payStage
            })
          }else{
            _this.setData({
              pay_type_status: res.data.pay_type_status,
              role: res.data.role,
            })
          }
        }
      }, (err) => {
        wx.hideLoading()
        wx.showToast({
          title: err.data.message,
          icon: 'none',
          duration: 2000
        })
      })
    // }
  },
  // 选择支付方式
  bindPickerChange:function(e){
    var index = e.detail.value
    var { payStage, price } = this.data
    payStage = []
    if (index == 0) {
      payStage = [{
        stage: '第一阶段',
        proportion: '100',
        amount: price,
        payRemarks: ''
      }]
    }else if(index == 1){
      payStage = [{
        stage: '第一阶段',
        proportion: '50',
        amount: price / 2,
        payRemarks: ''
      }, {
        stage: '第二阶段',
        proportion: '50',
        amount: price / 2,
        payRemarks: ''
      }]
    }else if(index == 2){
      payStage = [{
        stage: '第一阶段',
        proportion: '50',
        amount: price * 0.5,
        payRemarks: ''
      }, {
        stage: '第二阶段',
        proportion: '30',
        amount: price * 0.3,
        payRemarks: ''
      }, {
        stage: '第三阶段',
        proportion: '20',
        amount: price * 0.2,
        payRemarks: ''
      }]
    }
    this.setData({
      index,
      payStage
    })
  },
  // 备注
  payRemarks:function(e){
    var { payStage } = this.data
    var { index } = e.target.dataset
    payStage[index].payRemarks = e.detail.value
    this.setData({
      payStage
    })
  },
  // 自定义
  customizeInp:function(e){
    this.setData({
      customizeVal:e.detail.value
    })
  },
  // 确定自定义格式
  customizeBtn:function(){
    var { customizeVal, payStage, price } = this.data
    var customizeVal = customizeVal.split(':');
    var customizeNum = 0;
    for (var i = 0; i < customizeVal.length;i++){
      customizeNum = parseInt(customizeNum) + parseInt(customizeVal[i])
    }
    if (customizeNum != 100){
      wx.showToast({
        title: '自定义比例总和应为100',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    payStage = [];
    var obj = {}
    for (var i = 0; i < customizeVal.length; i++){
      obj = {
        stage: '第' + (i + 1) +'阶段',
        proportion: customizeVal[i],
        amount: price * (customizeVal[i] / 100),
        payRemarks: ''
      }
      payStage.push(obj)
    }
    this.setData({
      payStage
    })
  },
  // 提交
  payTypeSubmitBtn:function(){
    var _this = this
    var url = app.globalData.baseUrl + 'myTask/postPayType';
    var { id, index, customizeVal, payStage, submit_again } = this.data;
    var desc = [];
    for (var i = 0; i < payStage.length; i++){
      desc.push(payStage[i].payRemarks)
    }

    desc = JSON.stringify(desc)
    desc = desc.replace(/,/g, ":")
    desc = desc.replace(/"/g, "")
    desc = desc.replace(/\[|]/g, "")
    var data = {
      task_id: id,
      token: app.globalData.token,
      pay_type: parseInt(index) + 1,
      desc: desc,
      pay_type_append: customizeVal,
      submit_again: submit_again
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxPostRequest(url, data, (res) => {
      wx.hideLoading()
      console.log(res)
      if (res.code == 1000) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },2000)
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 威客是否同意支付方式
  wkOperatingBtn:function(e){
    // console.log(e)
    var { type } = e.currentTarget.dataset
    var url = app.globalData.baseUrl + 'myTask/dealPayType';
    var { id } = this.data;
    var data = {
      task_id: id,
      token: app.globalData.token,
      type: type
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data, (res) => {
      wx.hideLoading()
      console.log(res)
      if (res.code == 1000) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
